﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace CIM.App_Code.DataAccess
{
    public static class ConnectionManager
    {
        private static SqlConnection m_connection = null;
        private static string _strConnectionString = null;
        #region Properties
        public static SqlConnection Connection
        {
            get
            {
                if ( m_connection == null || string.IsNullOrEmpty(m_connection.ConnectionString) )
                {
                    return GetConnection();    
                }
                else
                {
                   
                    return m_connection;
                }
                
            }

        }

        //Get Connection string from the application config file
        private static SqlConnection GetConnection()
        {           
            if(_strConnectionString == null)
            {
                AppSettingsReader appSetting = new AppSettingsReader();
                _strConnectionString = appSetting.GetValue("connectionString", typeof(System.String)).ToString();
            }
            m_connection = new SqlConnection(_strConnectionString);
            return m_connection;

        }

        #endregion Properties
             
    }

    public class DataContext{
        
        private SqlConnection connection;
        private SqlTransaction transaction;
        
        private static DataContext instance = null;

        public static DataContext GetInstance()
        {            
            return instance;
        }

        public DataContext()
        {
            if (this.connection == null || string.IsNullOrEmpty(this.connection.ConnectionString))
            {
                this.connection = ConnectionManager.Connection;                            
            }            
            instance = this;
        }

        public DataContext(SqlConnection con, SqlTransaction tran)
        {            
            if(con == null){
                throw new Exception("A null SqlConnection was given to the Data Context!");                
            }
            this.connection = con;
            this.transaction = tran;
            instance = this;
        }
        
        public SqlTransaction Transaction{
            get{
                return this.transaction;
            }            
        }
        
        public void BeginTransaction()
        {
            if (this.connection.State != System.Data.ConnectionState.Open)
            {
                this.connection.Open();
            }
            if(this.transaction == null)
            {
                this.transaction = this.connection.BeginTransaction();
            }                        
        }

        public void Commit(){
            
            if(this.transaction == null){
                throw new Exception("There is no transaction to commit!");
            }
            else{
                this.transaction.Commit();
                this.transaction = null;
                this.connection.Close();
                this.connection.Dispose();
            }            
        }

        public void Rollback(){
            
            if(this.transaction == null){
                throw new Exception("There is no transaction to rollback!");
            }
            else
            {
                this.transaction.Rollback();
                this.transaction = null;
                this.connection.Close();
                this.connection.Dispose();
            }            
        }
    }

}
